package com.huiren.ui.configUI;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Date;
import java.util.List;
import javax.annotation.PostConstruct;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.JComboBox;
import javax.swing.JButton;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Component;
import com.huiren.util.JdbcUtil;
import com.huiren.util.PropertiesUtil;
import com.huiren.util.XMLUtil;

import java.awt.Color;

@Component
public class JDBCConfigJPanel extends JPanel {

	private static Logger logger = Logger.getLogger(JDBCConfigJPanel.class);
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private JTextField url;
	private JTextField user;
	private JTextField password;
	private JLabel dbtest;
	private JComboBox dbType;

	public JDBCConfigJPanel() {

	}

	private int initData(String type) {
		XMLUtil xmlUtil = new XMLUtil();

		List<String> sql0 = xmlUtil.selectNodes("/sql/" + type
				+ "/createPurOrder");
		List<String> sql1 = xmlUtil.selectNodes("/sql/" + type
				+ "/createConfig");
		List<String> sql2 = xmlUtil.selectNodes("/sql/defaultInsert");
		sql0.addAll(sql1);
		sql0.addAll(sql2);
		return JdbcUtil.executeUpdate(sql0);
	}

	private void save() {
		PropertiesUtil propertie = new PropertiesUtil();
		propertie.setValue("jdbc.driver", this.getDriver(this.dbType));
		propertie.setValue("jdbc.driverUrl", this.url.getText());
		propertie.setValue("jdbc.user", this.user.getText());
		propertie.setValue("jdbc.password", this.password.getText());
		propertie.saveFile(new Date().toString());
	}

	private void reSet() {
		PropertiesUtil propertie = new PropertiesUtil();
		if ("oracle.jdbc.driver.OracleDriver".equals(propertie
				.getValue("jdbc.driver")))
			dbType.setSelectedIndex(0);
		if ("net.sourceforge.jtds.jdbc.Driver".equals(propertie
				.getValue("jdbc.driver")))
			dbType.setSelectedIndex(1);
		if ("com.mysql.jdbc.Driver".equals(propertie.getValue("jdbc.driver")))
			dbType.setSelectedIndex(2);

		this.url.setText(propertie.getValue("jdbc.driverUrl"));
		this.user.setText(propertie.getValue("jdbc.user"));
		this.password.setText(propertie.getValue("jdbc.password"));
	}

	private String getDriver(JComboBox dbType) {
		String driver = "";
		switch (dbType.getSelectedIndex()) {
		case 0:
			driver = "oracle.jdbc.driver.OracleDriver";
			break;
		case 1:
			driver = "net.sourceforge.jtds.jdbc.Driver";
			break;
		case 2:
			driver = "com.mysql.jdbc.Driver";
			break;
		}
		return driver;
	}

	@PostConstruct
	public void initMethod() {
		setLayout(null);

		JLabel lblNewLabel_0 = new JLabel("类  型:");
		lblNewLabel_0.setBounds(27, 61, 51, 15);
		add(lblNewLabel_0);

		JLabel lblNewLabel_1 = new JLabel("地  址:");
		lblNewLabel_1.setBounds(27, 107, 51, 15);
		add(lblNewLabel_1);

		JLabel lblNewLabel_2 = new JLabel("用户名:");
		lblNewLabel_2.setBounds(27, 157, 51, 15);
		add(lblNewLabel_2);

		JLabel lblNewLabel_3 = new JLabel("密  码:");
		lblNewLabel_3.setBounds(27, 205, 51, 15);
		add(lblNewLabel_3);

		dbType = new JComboBox();
		dbType.setBounds(87, 58, 105, 21);
		dbType.setModel(new DefaultComboBoxModel(new String[] { "oracle",
				"sqlserver", "mysql" }));
		add(dbType);

		url = new JTextField();
		url.setBounds(88, 104, 290, 21);
		add(url);
		url.setColumns(10);

		user = new JTextField();
		user.setBounds(88, 154, 290, 21);
		add(user);
		user.setColumns(10);

		password = new JTextField();
		password.setBounds(88, 202, 290, 21);
		add(password);
		password.setColumns(10);

		dbtest = new JLabel("");
		dbtest.setForeground(Color.RED);
		dbtest.setBounds(157, 270, 393, 15);
		add(dbtest);

		JButton btnNewButton = new JButton("保  存");
		btnNewButton.setBounds(37, 306, 93, 23);
		add(btnNewButton);

		btnNewButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				int type = JOptionPane.showConfirmDialog(null, "您确定要保存吗?",
						"提示", JOptionPane.YES_NO_OPTION,
						JOptionPane.QUESTION_MESSAGE);
				if (type == JOptionPane.YES_NO_OPTION) {
					JDBCConfigJPanel.this.save();
					dbtest.setText("保存成功");
					logger.info("保存数据链接");
				}
			}
		});

		JButton btnNewButton_1 = new JButton("测试连接");
		btnNewButton_1.setBounds(37, 266, 93, 23);
		add(btnNewButton_1);

		btnNewButton_1.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				dbtest.setText(JdbcUtil.test(
						JDBCConfigJPanel.this.getDriver(dbType), url.getText(),
						user.getText(), password.getText()));
			}
		});
		JButton btnNewButton_2 = new JButton("重  置");
		btnNewButton_2.setBounds(267, 306, 93, 23);
		add(btnNewButton_2);
		btnNewButton_2.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				JDBCConfigJPanel.this.reSet();
			}
		});

		JButton btnNewButton_3 = new JButton("创建基本表与数据");
		btnNewButton_3.setBounds(37, 353, 150, 23);
		add(btnNewButton_3);
		btnNewButton_3.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				int type = JOptionPane
						.showConfirmDialog(null, "表中现有数据将被清空重置,您确定要创建吗?", "提示",
								JOptionPane.YES_NO_OPTION,
								JOptionPane.QUESTION_MESSAGE);
				if (type == JOptionPane.YES_NO_OPTION) {
					if (initData(dbType.getSelectedItem().toString()) != -1)
						dbtest.setText("创建成功");
				}
			}
		});
		reSet();
	}
}
